﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Common.Utils
{
    public class ApiResponse<T>
    {
        /// <summary>
        /// 请求是否成功
        /// </summary>
        [JsonPropertyName("isSuccess")]
        public bool IsSuccess { get; set; }

        /// <summary>
        /// 返回消息
        /// </summary>
        [JsonPropertyName("message")]
        public string Message { get; set; }

        /// <summary>
        /// 返回数据
        /// </summary>
        [JsonPropertyName("data")]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
        public T? Data { get; set; }

        /// <summary>
        /// 状态码
        /// </summary>
        [JsonPropertyName("code")]
        public int Code { get; set; }

        /// <summary>
        /// 时间戳
        /// </summary>
        [JsonPropertyName("timestamp")]
        public DateTime Timestamp { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 请求ID（用于日志追踪）
        /// </summary>
        //public string RequestId { get; set; } = Guid.NewGuid().ToString();

        #region 成功方法

        public static ApiResponse<T> Success(T data, string message = "操作成功")
        {
            return new ApiResponse<T> { IsSuccess = true, Data = data, Message = message, Code = 200 };
        }

        // 成功方法 - 无数据（用于泛型版本）
        public static ApiResponse<T> Success(string message = "操作成功")
        {
            return new ApiResponse<T> { IsSuccess = true, Message = message, Code = 200 };
        }

        // 失败方法 - 不返回数据
        public static ApiResponse<T> Fail(string message, int code = 400)
        {
            return new ApiResponse<T> { IsSuccess = false, Message = message, Code = code };
        }



        /// <summary>
        /// 未授权失败
        /// </summary>
        /// <param name="message">错误消息</param>
        public static ApiResponse<T> Unauthorized(string message = "未授权访问")
        {
            return new ApiResponse<T>
            {
                IsSuccess = false,
                Message = message,
                Code = 401
            };
        }

        /// <summary>
        /// 禁止访问失败
        /// </summary>
        /// <param name="message">错误消息</param>
        public static ApiResponse<T> Forbidden(string message = "禁止访问")
        {
            return new ApiResponse<T>
            {
                IsSuccess = false,
                Message = message,
                Code = 403
            };
        }

        /// <summary>
        /// 未找到资源失败
        /// </summary>
        /// <param name="message">错误消息</param>
        public static ApiResponse<T> NotFound(string message = "资源未找到")
        {
            return new ApiResponse<T>
            {
                IsSuccess = false,
                Message = message,
                Code = 404
            };
        }

        /// <summary>
        /// 服务器错误失败
        /// </summary>
        /// <param name="message">错误消息</param>
        public static ApiResponse<T> ServerError(string message = "服务器内部错误")
        {
            return new ApiResponse<T>
            {
                IsSuccess = false,
                Message = message,
                Code = 500
            };
        }

        #endregion




    }
}
